using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

[Serializable]
public class MissionRecords
{
	[System.Serializable]
	public class MRecord
	{
		public int HighScore;

		public int Star;
	}

	public Dictionary<string, MRecord> Records = new Dictionary<string, MRecord>();

	public Dictionary<int, int> openLevel = new Dictionary<int, int>();

	public List<StringAndMRecordKeyValue> recordsList = new List<StringAndMRecordKeyValue>();
	public List<IntAndIntKeyValue> openLevelList = new List<IntAndIntKeyValue>();

	public string ToJson()
	{
		recordsList.Clear();
		foreach (KeyValuePair<string, MRecord> record in Records)
		{
			recordsList.Add(new StringAndMRecordKeyValue(record.Key, record.Value));
		}
		openLevelList.Clear();
		foreach (KeyValuePair<int, int> v in openLevel)
		{
			openLevelList.Add(new IntAndIntKeyValue(v.Key, v.Value));
		}
		return JsonUtility.ToJson(this);
	}

	public void JsonToObject(string json)
	{
		MissionRecords missionRecords = JsonUtility.FromJson<MissionRecords>(json);
		if (missionRecords == null) return;
		Records.Clear();
		foreach (StringAndMRecordKeyValue v in missionRecords.recordsList)
		{
			Records.Add(v.MRecordKey, v.MRecordValue);
		}
		openLevel.Clear();
		foreach (IntAndIntKeyValue v in missionRecords.openLevelList)
		{
			openLevel.Add(v.Key, v.Value);
		}
	}

	public void Record(string name, int star, int score)
	{
		if (!Records.ContainsKey(name))
		{
			MRecord mRecord = new MRecord();
			mRecord.Star = star;
			mRecord.HighScore = score;
			MRecord value = mRecord;
			Records.Add(name, value);
		}
		else
		{
			MRecord value = Records[name];
			value.HighScore = score;
			value.Star = star;
		}
		int chapter = ConstantData.LevelDatas.GetChapter(name);
		int level = ConstantData.LevelDatas.GetLevel(name) + 1;
		RecordOpenLevel(chapter, level);
	}

	public int GetOpenLevel(int chapter)
	{
		if (openLevel.ContainsKey(chapter))
		{
			return openLevel[chapter];
		}
		return 1;
	}

	public void RecordOpenLevel(int chapter, int level)
	{
		if (openLevel.ContainsKey(chapter))
		{
			if (openLevel[chapter] < level)
			{
				openLevel[chapter] = level;
			}
		}
		else
		{
			openLevel.Add(chapter, level);
		}
	}

	public int GetTotalStar()
	{
		int num = 0;
		foreach (KeyValuePair<string, MRecord> record in Records)
		{
			num += record.Value.Star;
		}
		return num;
	}

	public int GetTotalScore()
	{
		int num = 0;
		foreach (KeyValuePair<string, MRecord> record in Records)
		{
			num += record.Value.HighScore;
		}
		return num;
	}

	public int GetHighScore(string name)
	{
		if (Records.ContainsKey(name))
		{
			return Records[name].HighScore;
		}
		return 0;
	}

	public int GetStar(string name)
	{
		if (Records.ContainsKey(name))
		{
			return Records[name].Star;
		}
		return 0;
	}

	public int GetChapterStars(int chapter)
	{
		int num = 0;
		if (openLevel.ContainsKey(chapter))
		{
			int num2 = openLevel[chapter];
			for (int i = 1; i < num2; i++)
			{
				string name = chapter + "-" + i;
				num += GetStar(name);
			}
		}
		return num;
	}
}
